// pages/repair/report/index.js
var app = getApp();
let urlIp = app.globalData.urlIp;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight: "",//窗口高度
    type: "查看",
    // 报告
    id: '',
    reportData: {
      partsConsume:[],
      downList:[],
      travelTimeList:[],
      hoursList:[],
    },
    partsConsume: [], // 备件消耗
    downList: [], // 宕机时间
    travelTimeList: [],// 差旅时间
    hoursList: [], // 工时
  },
  createBtn() {
    this._createReport();
  },
  signBtn() {
    this._signReport();
  },
  // 生成报告
  _createReport() {
    let obj = {};
    obj.id = this.data.id;
    this.axios
      .custom("RepairWorkOrderReport", "createReport", obj)
      .then((res) => {
        // 正常的话直接跳转回列表页
        if (res.code == '200') {
          let that = this // 缓存作用域
          // 添加成功跳转到详情页
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000,
            success() {
              that.Router.push({
                path: 'workLog',
                query: {},
                openType: 'redirectTo'
              });
              console.log('准备跳转')
            }
          })
        } else {
          this.showModal(res)
        }
        console.log(res, "签收报告");
      })
      .catch((e) => {
        console.log(e, "错误信息");
      });
  },
  // 签收报告
  _signReport() {
    let obj = {};
    obj.id = this.data.id;
    this.axios
      .custom("RepairWorkOrderReport", "signReport", obj)
      .then((res) => {
        if (res.code == '200') {
          let that = this // 缓存作用域
          // 添加成功跳转到详情页
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000,
            success() {
              that.Router.push({
                path: 'workLog',
                query: {},
                openType: 'redirectTo'
              });
              console.log('准备跳转')
            }
          })
        } else {
          this.showModal(res)
        }
        console.log(res, "签收报告");
      })
      .catch((e) => {
        console.log(e, "错误信息");
      });
  },
  // 获取报告数据
  _getData() {
    let obj = {}
    obj.id = this.data.id;
    this.axios.query("RepairWorkOrderReport", {}, obj, "")
      .then((res) => {
        console.log(res, "报告数据");
        if (res.code == '200') {
          if (res.data.rows[0]) {
            let temp = res.data.rows[0];
            if(temp.engineerSignatureUrl){
              temp.engUrl = urlIp + temp.engineerSignatureUrl
            }else{
              temp.engUrl = '';
            }
            if(temp.userSignatureUrl){
              temp.userUrl = urlIp + temp.userSignatureUrl
            }else{
              temp.userUrl = '';
            }
            this.setData({
              reportData: temp
            })
          }
        } else {
          this.showModal(res)
        }
      });
  },
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  // 设置按钮权限
  manageCon() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 300;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    console.log(options.type,'传递的数据')
    this.data.id = options.id;
    this.setData({
      type: options.type
    })
    this._getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})